var gulp = require('gulp')
    changed = require('gulp-changed'),
    htmlmin = require('gulp-htmlmin'),
    imagemin = require('gulp-imagemin'),
    uglify = require('gulp-uglify'),
    rev = require('gulp-rev'),
    rev_collector = require('gulp-rev-collector'),
    server = require('browser-sync'),
    babel = require('gulp-babel'),
    sourcemaps = require('gulp-sourcemaps'),
    pump = require('pump'),
    config = require('./gulp.config'),
    override=require('gulp-rev-css-url')
    proxyMiddleware = require('http-proxy-middleware')
    del = require('del')
    runSequence = require('run-sequence')
    tinypng = require('gulp-tinypng')
    reload = server.reload;

var baseDir = config.baseDir
var port = config.port

// var apiProxy = proxyMiddleware('/index.php', {
//     target:'http://localhost:3000',
//     changeOrigin: true
// })

// 设置服务器
gulp.task('server', () => {
    server.init({
        port: port,
        server: {
            baseDir: baseDir,
            index: config.html
        },
        // middleware: apiProxy
    })
})

// 复制html
gulp.task('html', () => {
    return gulp.src('./src/**/*.html')
        .pipe(changed(baseDir))
        .pipe(htmlmin({
            collapseWhitespace: false,
            removeComments: true
        }))
        .pipe(gulp.dest(baseDir))
})

gulp.task('js', () => {
    return gulp.src('./src/**/*.js')
        .pipe(sourcemaps.init())
        .pipe(changed(baseDir))
        .pipe(babel())
        .pipe(sourcemaps.write('maps'))
        .pipe(gulp.dest(baseDir))
})

gulp.task('css', () => {
    return gulp.src('./src/**/*.css')
        .pipe(changed(baseDir))
        .pipe(gulp.dest(baseDir))
})

gulp.task('img', () => {
    return gulp.src('./src/**/*.{jpg,png,gif,ico}')
        .pipe(changed(baseDir))
        .pipe(tinypng('iOM-o5i6GNe_N0LIWr8GbKLmUvL1gQc8'))
        .pipe(gulp.dest(baseDir))
})

gulp.task('font', () => {
    return gulp.src('./src/**/*.{svg,ttf,woff,eot}')
        .pipe(gulp.dest(baseDir))
})

gulp.task('revision', () => {
    return gulp.src(['./dist/**/*.js', './dist/**/*.css', './dist/**/*.{jpg,png,gif,ico}'])
        .pipe(rev())
        .pipe(override())
        .pipe(gulp.dest('./build'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('./build'))
})

gulp.task('replace', ['revision'], () => {
    gulp.src(['./build/rev-manifest.json', './dist/**/*.html'])
        .pipe(rev_collector({
            replaceReved: true
        }))
        .pipe(gulp.dest('./build'))
})


gulp.task('watch', () => {
    gulp.watch('src/**/*.js', ['js'])
    gulp.watch('src/**/*.css', ['css'])
    gulp.watch('src/**/*.{jpg,png,gif,ico}', ['img'])
    gulp.watch('src/**/*.html', ['html'])
    gulp.watch('dist/**/*').on('change', reload)
});

// 清空dist

gulp.task('clean', () => {
    console.log('clean')
    return del([baseDir + '/**/*', './build/**/*'])
})

gulp.task('dev', ['html', 'css', 'img', 'js', 'font', 'server', 'watch'])

gulp.task('build', (cb) => {
    runSequence('clean', ['html', 'css', 'img', 'js', 'font'], 'revision', 'replace', cb)
})
